﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace LaTeXGrapher
{
    public class ShadeData
    {
        KeyValuePair<string, string> _domain;

        public string Name { get; private set; }

        public ShadeData(string name, KeyValuePair<string,string> domain)
        {
            Name = name;
            _domain = domain;
        }

        public static ShadeData Create(IEnumerable<string> arguments)
        {
            var array = arguments.ToArray();
            if (array.Length != 2)
                throw new Exception("Expected two arguments to Shade in: Shade " + string.Join(", ", array));
            if (!Tokenizer.IsIdentifier(array[0].Trim()))
                throw new Exception("First arguments in Shade must be an identifier: Shade " + string.Join(", ", array));
            var pairs = FunctionParser.ParseListOfPairs(array[1]).ToArray();
            if (pairs.Length != 1)
                throw new Exception("Second argument in Shade must be a pair: Shade " + string.Join(", ", array));
            return new ShadeData(array[0].Trim(), pairs[0]);
        }

        public KeyValuePair<double, double> GetShadeDomain(FunctionContext context)
        {
            return LiftP<string,double>(context.Evaluate, _domain);
        }

        static KeyValuePair<U, U> LiftP<T, U>(Func<T, U> f, KeyValuePair<T, T> pair)
        {
            return new KeyValuePair<U, U>(f(pair.Key), f(pair.Value));
        }
    }
}
